package com.senseuni.da.utils

import android.content.Context
import android.hardware.usb.UsbManager
import com.elvishew.xlog.XLog
import com.senseuni.da.model.ExecResult
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.IOException
import java.io.InputStreamReader
import java.lang.reflect.Method


/**
 * ================================================
 * Copyright (c) 2020 All rights reserved
 * 描述：
 * Author: Yuri
 * Date： Date: 2024/12/12
 * ================================================
 */
object ShellUtil {

    fun execSU(command: String) : ExecResult{
        XLog.d("execSU:${command}")
        var process: Process? = null
        var reader: BufferedReader? = null
        var isr: InputStreamReader? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec("su")
            isr = InputStreamReader(process.inputStream)
            reader = BufferedReader(isr)
            os = DataOutputStream(process.outputStream)
            os.writeBytes("${command}\n")
            os.writeBytes("exit\n")
            os.flush()
            var read: Int
            val buffer = CharArray(4096)
            val output = StringBuilder()
            while (reader.read(buffer).also { read = it } > 0) {
                output.append(buffer, 0, read)
            }
            process.waitFor()
            output.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            XLog.e(command + " exec fail:" + e.message)
            return ExecResult(-1, e.message ?: "exec fail")
        } finally {
            try {
                os?.close()
                isr?.close()
                reader?.close()
                process?.destroy()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        XLog.d("$command exec success")
        return ExecResult(0, "success")
    }

    fun exec(command: String) : ExecResult{
        XLog.d("exec:${command}")
        var process: Process? = null
        var reader: BufferedReader? = null
        var isr: InputStreamReader? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec(command)
            isr = InputStreamReader(process.inputStream)
            reader = BufferedReader(isr)
            var read: Int
            val buffer = CharArray(4096)
            val output = StringBuilder()
            while (reader.read(buffer).also { read = it } > 0) {
                output.append(buffer, 0, read)
            }
            process.waitFor()
            output.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            XLog.e(command + " exec fail:" + e.message)
            return ExecResult(-1, e.message ?: "exec fail")
        } finally {
            try {
                os?.close()
                isr?.close()
                reader?.close()
                process?.destroy()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        XLog.d("$command exec success")
        return ExecResult(0, "success")
    }


}